{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 3, 4, 12, 59, 36, 872492)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 今天\n",
    "today = datetime.datetime.now()\n",
    "today"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 3, 3, 12, 59, 36, 872492)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 昨天\n",
    "yesterday = today - datetime.timedelta(days=1)\n",
    "yesterday"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2020, 3, 4, 12, 59, 36, 872492)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去年同期（不考虑闰年）\n",
    "if today.month == 2 and today.day == 29:\n",
    "    last_month = today.replace(year=today.year - 1,day=28)\n",
    "else:\n",
    "    last_year = today.replace(year=today.year - 1)\n",
    "last_year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 2, 4, 12, 59, 36, 872492)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上月同期（不考虑闰年）\n",
    "if today.month == 1:\n",
    "    last_month = today.replace(year=today.year - 1,month=12)\n",
    "elif today.month in [5,7,10,12] and today.day == 31:\n",
    "    last_month = today.replace(month=today.month - 1,day=30)\n",
    "elif today.month == 3 and today.day in [29,30,31]:\n",
    "    last_month = today.replace(month=today.month - 1,day=28)\n",
    "else:\n",
    "    last_month = today.replace(month=today.month - 1)\n",
    "last_month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断闰年\n",
    "def func(years):\n",
    "    if ((years % 4 == 0 and years % 100 != 0) or (years % 400 == 0)):  # 判断是否是闰年\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "    \n",
    "func(2021)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 3, 1, 12, 59, 36, 872492)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本周第一天\n",
    "this_week_first = today - datetime.timedelta(days=today.weekday())\n",
    "this_week_first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 3, 7, 12, 59, 36, 872492)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本周最后一天\n",
    "this_week_last = today + datetime.timedelta(days=6 - today.weekday())\n",
    "this_week_last"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 2, 22, 12, 59, 36, 872492)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上周第一天\n",
    "last_week_first = today - datetime.timedelta(days=today.weekday() + 7)\n",
    "last_week_first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 2, 28, 12, 59, 36, 872492)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上周最后一天\n",
    "last_week_last = today - datetime.timedelta(days=today.weekday() + 1)\n",
    "last_week_last"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 3, 1, 0, 0)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本月第一天\n",
    "this_month_first = datetime.datetime(today.year, today.month, 1)\n",
    "this_month_first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 3, 31, 0, 0)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本月最后一天\n",
    "this_month_last = datetime.datetime(today.year, today.month + 1, 1) - datetime.timedelta(days=1)\n",
    "this_month_last"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 2, 28, 0, 0)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#上月最后一天\n",
    "last_month_last = this_month_first - datetime.timedelta(days=1)\n",
    "last_month_last"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 2, 1, 0, 0)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上月第一天\n",
    "last_month_first = datetime.datetime(last_month_last.year, last_month_last.month, 1)\n",
    "last_month_first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 1, 1, 0, 0)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本季第一天\n",
    "month = (today.month - 1) - (today.month - 1) % 3 + 1\n",
    "this_quarter_first = datetime.datetime(today.year, month, 1)\n",
    "this_quarter_first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 3, 31, 0, 0)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本季最后一天\n",
    "this_quarter_last = datetime.datetime(today.year, month + 3, 1) - datetime.timedelta(days=1)\n",
    "this_quarter_last"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2020, 12, 31, 0, 0)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上季最后一天\n",
    "last_quarter_last = this_quarter_first - datetime.timedelta(days=1)\n",
    "last_quarter_last"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2020, 10, 1, 0, 0)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上季第一天\n",
    "last_quarter_first = datetime.datetime(last_quarter_last.year, last_quarter_last.month - 2, 1)\n",
    "last_quarter_first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 1, 1, 0, 0)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本年第一天\n",
    "this_year_first = datetime.datetime(today.year, 1, 1)\n",
    "this_year_first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 12, 31, 0, 0)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本年最后一天\n",
    "this_year_last = datetime.datetime(today.year + 1, 1, 1) - datetime.timedelta(days=1)\n",
    "this_year_last"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2020, 12, 31, 0, 0)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去年最后一天\n",
    "last_year_last = this_year_first - datetime.timedelta(days=1)\n",
    "last_year_last"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2020, 1, 1, 0, 0)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去年第一天\n",
    "last_year_first = datetime.datetime(last_year_last.year, 1, 1)\n",
    "last_year_first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "63"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 当年第几天\n",
    "int(today.strftime('%j')) # %j十进制表示的每年的第几天"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 当年第几周\n",
    "int(today.strftime('%W')) # %j返回当前时间对应的当年周序号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 当月第几周\n",
    "begin = int(this_month_first.strftime(\"%W\"))\n",
    "end = int(today.strftime(\"%W\"))\n",
    "\n",
    "end - begin + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 当年第几季\n",
    "def season(x):\n",
    "    if x in [3,4,5]:\n",
    "        return 1\n",
    "    elif x in [6,7,8]:\n",
    "        return 2\n",
    "    elif x in [9,10,11]:\n",
    "        return 3\n",
    "    elif x in [12,1,2]:\n",
    "        return 4\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "\n",
    "month = int(today.strftime('%m'))\n",
    "season(month)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7200, 11066400.0, 128)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 两个日期之差\n",
    "a = datetime.datetime.strptime('2020-10-27 10:30:00','%Y-%m-%d %H:%M:%S')\n",
    "b = datetime.datetime.strptime('2021-03-04 12:30:00','%Y-%m-%d %H:%M:%S')\n",
    "\n",
    "minus = b - a\n",
    "\n",
    "# 秒（seconds 获得的秒只是时间差中的小时、分钟和秒部分的和，并没有包含时间差的天数）\n",
    "minus.seconds\n",
    "# 秒（total_seconds() 获得的秒包含时间差的天数）\n",
    "minus.total_seconds()\n",
    "# 天\n",
    "minus.days\n",
    "\n",
    "minus.seconds,minus.total_seconds(),minus.days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 0.03333333333333333, 0.0005555555555555556, 2.3148148148148147e-05)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 两个时间之差\n",
    "a = datetime.datetime.now()\n",
    "time.sleep(2)\n",
    "b = datetime.datetime.now()\n",
    "\n",
    "minus = b - a\n",
    "\n",
    "# 秒\n",
    "minus.seconds\n",
    "# 分\n",
    "minus.seconds / 60\n",
    "# 小时\n",
    "minus.seconds / 60 / 60\n",
    "# 天\n",
    "minus.seconds / 60 / 60 / 24\n",
    "\n",
    "minus.seconds,minus.seconds / 60,minus.seconds / 60 / 60,minus.seconds / 60 / 60 / 24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(datetime.datetime(2021, 3, 14, 12, 59, 41, 438965),\n",
       " 'Sun Mar 14 12:59:41 2021')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间相加\n",
    "d1 = datetime.datetime.now()\n",
    "d2 = d1 + datetime.timedelta(days=10)\n",
    "\n",
    "d2,d2.ctime()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
